package com.example.mei.blueautosend.chart;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mei.blueautosend.R;
import com.example.mei.blueautosend.SaveData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MEI on 2018/3/9.
 */

public class OvlChart extends Activity implements View.OnClickListener{
    List<JiSuLinePoint> list_all_data;
    List<JiSuLinePoint> list_data;
    JiSuLineView lineView;
    List<XAxisScale> xAxisScaleList;
    int count = 1;
    private int min = 0;
    private int max = 1000;
    EditText min_txt;
    EditText max_txt;
    TextView yes;
    int dayBetween = 20;
    private int dataCount = 20;
    private String Path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.chart_view);
        lineView = (JiSuLineView) findViewById(R.id.fl_content);
        min_txt = (EditText) findViewById(R.id.min);
        max_txt = (EditText) findViewById(R.id.max);
        yes = (TextView) findViewById(R.id.yes);
        yes.setOnClickListener(this);
        list_all_data = new ArrayList<JiSuLinePoint>(); //文件内容字符串
        xAxisScaleList = new ArrayList<>();
        list_data = new ArrayList<>();
        Path = getSDPath() + File.separator + SaveData.FILE_NAME;
        init();

        lineView.initData(xAxisScaleList,list_data,20,dayBetween,min,max);
    }

    public void init(){
        ReadTxtFile();
        initXAxisScale();
    }

    public void initXAxisScale(){
        count = 1;
        xAxisScaleList.clear();
        list_data.clear();
        if(list_all_data.size()!=0) {
            for (int i = list_all_data.size() - 1; i >=0; i--) {

                XAxisScale xAxisScale = new XAxisScale(count, (int) (count / 20.0 * 100));

                list_data.add(list_all_data.get(count - 1));

                xAxisScaleList.add(xAxisScale);
                count++;
                if (xAxisScaleList.size() == 20) {

                    break;
                }
            }
        }
    }

    private String getSDPath() {
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) {
            return Environment.getExternalStorageDirectory().toString();
        } else {
            return Environment.getDownloadCacheDirectory().toString();
        }
    }

    public void ReadTxtFile() {


        list_all_data.clear();
        //打开文件
        File file = new File(Path);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory()) {
            Log.d("TestFile", "The File doesn't not exist.");
        } else {
            try {
                InputStream instream = new FileInputStream(file);

                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line;
                //分行读取
                while ((line = buffreader.readLine()) != null) {
                    if (!line.trim().equals("")) {
                        System.out.println("读取的值"+line);
                        String line_txt = line.replace("阻抗:","").replace("温度:","")
                                .replace("测试条件:","");
                        JiSuLinePoint point = new JiSuLinePoint();
                        for (int i = 0; i <line_txt.split("\\|").length ; i++) {
                            if(i==0) {
                                String data = line_txt.split("\\|")[i];
                                point.setData(Integer.parseInt(data));
                                point.setPercentDataY((Integer.parseInt(data)-min)/(max-(float)min)*100.0);
                            }else if(i==1){
                                double tmp = Double.parseDouble(line_txt.split("\\|")[i].trim());
                                point.setTmp(tmp);
                                point.setPercentTmpY(tmp/45.0*100.0);
                            }else{
                                point.setTestInfo(line_txt.split("\\|")[i].trim());
                            }
                        }
                        list_all_data.add(point);
                    }
                }
                instream.close();

            } catch (FileNotFoundException e) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException e) {
                Log.d("TestFile", e.getMessage());
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yes:
                if(Integer.parseInt(max_txt.getText().toString())>Integer.parseInt(min_txt.getText().toString())){
                    max = Integer.parseInt(max_txt.getText().toString());
                    min = Integer.parseInt(min_txt.getText().toString());
                    init();
                    lineView.setXaxisScale(xAxisScaleList,list_data,min,max);
                }else{
                    Toast.makeText(this,"无效的值",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
